﻿using EquipmentManagement.Application.IServices;
using EquipmentManagement.Domain.Entities;
using Infrastructure;
using Standard;
using Standard.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace EquipmentManagement.Application.Services
{
    public class DeviceLedgerService : IDeviceLedgerService
    {
        private readonly MesRepository<DeviceLedger> _deviceledger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_deviceledger"></param>
        public DeviceLedgerService(MesRepository<DeviceLedger> deviceledger)
        {
            _deviceledger = deviceledger;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> Add(DeviceLedger model)
        {
            try
            {
                model.ID = YitIdHelper.NextId().ToString();
                model.AssetNumber = "XKY" + DateTime.Now.ToString("yyyyMMddHHmmssfff");
                model.CreatedUserName = "admin";
                model.CreatedTime = DateTime.Now;
                model.IsDeleted = false;
                model.State = "正常";
                var i = _deviceledger.Add(model);
                return new ApiResult<int>
                {
                    Code = ApiCode.Success,
                    ResultData = i,
                    Message = "添加成功"
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除(逻辑删除)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> Delete(string id)
        {
            try
            {
                var ids = id.Split(",");
                var models = _deviceledger.GetQueryable(x => x.IsDeleted == false).ToList();
                var list = new List<DeviceLedger>();
                foreach (var i in ids)
                {
                    list.Add(models.FirstOrDefault(x => x.ID == i)!);
                }
                list.ForEach(x => x.IsDeleted = true);
                _deviceledger.BatchUpdate(list);
                return new ApiResult<int>()
                {
                    Code = ApiCode.Success,
                    Message = "删除成功",
                    ResultData = 1
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<DeviceLedger> Get(string id)
        {
            try
            {
                return new ApiResult<DeviceLedger>
                {
                    Code = ApiCode.Success,
                    Message = "成功",
                    ResultData = _deviceledger.GetEntity(x => x.ID == id)
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取列表(分页+条件查询)
        /// </summary>
        /// <param name="paging"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiPaging<DeviceLedger> GetList(PagingInput paging, string? AssetNumber, string? Code, string? Name, string? State)
        {
            try
            {
                var model = _deviceledger.GetQueryable(x => x.IsDeleted == false);
                if (!string.IsNullOrEmpty(AssetNumber))
                {
                    model = model.Where(x => x.AssetNumber.Contains(AssetNumber));
                }
                if (!string.IsNullOrEmpty(Name))
                {
                    model = model.Where(x => x.Name.Contains(Name));
                }
                if (!string.IsNullOrEmpty(Code))
                {
                    model = model.Where(x => x.Code.Contains(Code));
                }
                if (!string.IsNullOrEmpty(State))
                {
                    model = model.Where(x => x.State == State);
                }
                return new ApiPaging<DeviceLedger>
                {
                    TotalCount = model.Count(),
                    TotalPage = (int)Math.Ceiling(model.Count() * 1.0 / paging.PageSize),
                    PageDatas = model.OrderByDescending(x => x.CreatedTime).Skip((paging.PageIndex - 1) * paging.PageSize).Take(paging.PageSize).ToList()
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public ApiResult<int> Update(DeviceLedger model)
        {
            try
            {
                model.UpdateTime = DateTime.Now;
                model.UpdateUserName = "admin";
                _deviceledger.Update(model);
                return new ApiResult<int>
                {
                    Code = ApiCode.Success,
                    Message = "修改成功",
                    ResultData = 1
                };
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
